<template>
	<div class="components-container">
        <div class="header-form">
            <div class="layui-inline">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-inline">
                  <el-input v-model="selectData.NameLike" placeholder="输入名称"></el-input>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">类别</label>
                <div class="layui-input-inline">
                  <el-select v-model="selectData.TypeEquals" placeholder="请选择类别">
                      <el-option v-for="item in UnttypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                  </el-select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">统计地址</label>
                <div class="layui-input-inline">
                  <el-cascader :options="UntyuseroupOptions" v-model="carryArea" filterable :show-all-levels="false" change-on-select>
                  </el-cascader>
                </div>
            </div>
            <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading" @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" @click="exportExcel" :disabled="this.$store.state.isloading">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <dataTable :height="height"  :headList="headList" :isIndex="true" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow" :hiddenPageOrder="false" :hiddenColumn="hiddenColumn"></dataTable>
        <printmodal ref="printPdf"></printmodal>
        <!-- 操作弹窗 -->
        <main-model ref="dialog" @refreshData="getData"></main-model>
	</div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import mainModel from "./ashUseMain";
const EXPORT = "打印";
export default {
  name: "unitManage",
  mixins: [manage],
  data() {
    return {
      selectData: {
      },
      downloadLoading:false,
      OutDataExcel:[],
      message: null,
      height: "620",  
      sweetModalWidth: 80,
      sweetModalMaxWidth: 1000,
      hiddenColumn: true,
      carryArea:[],
      UnttypeOptions:[],
      UntyuseroupOptions: [],
    };
  },
  methods: {
    exportExcel() {
      var startTime;
      var endTime;
      if(this.selectData.CdhactualdateGreaterAndEquals){
        startTime = this.selectData.CdhactualdateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.CdhactualdateLessAndEquals){
        endTime = this.selectData.CdhactualdateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      var header = '单位统计表' + '(' + startTime + '--' + endTime + ')';
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      console.log(this.headList);
      console.log(this.OutDataExcel);
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case EXPORT:
          this.$refs.printPdf.report(PRINT.PDFDRIVERS,this.getDataC());
          break;
        case '新增':
          this.$refs.dialog.open(this.order);
          break;
        case '修改':
          this.$refs.dialog.open(this.order, this.currentRow);
          break;
        case '详情':
          break;
        case '删除':
          break;
        default:
      }
    },
    getColumns() {
      let data = {
          columnName: '骨灰管理列表'
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
   getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if(Data.RegiskeypointEquals === null || Data.RegiskeypointEquals === undefined) Data.RegiskeypointEquals = 'A';
       Data.CdhactualdateGreaterAndEquals = formatDate(
        Data.CdhactualdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.CdhactualdateLessAndEquals = formatDate(
        Data.CdhactualdateLessAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: 1,
        PageSize: 100,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.UsergroupEquals = this.carryArea[this.carryArea.length - 1];
      let data = {
        PageIndex: 1,
        PageSize: 10,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Unit.Query,
          res => {
            // this.hiddenColumnInfo = true;
            this.dataList = res.data.content.OutData;
            this.OutDataExcel = res.data.content.OutData;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );
      
    },
  
    //当前选中行
    selectRow(row,order) {
      this.currentRow = row;
      this.$emit("selectRow", row, "");
      this.$refs.dialog.open(order,this.currentRow);
    },
    // dataRepeat(){},
    btnRepeat() {
        this.selectData = {};
        this.getData();
      },
    btnShow(btn){
        switch (btn) {
            case EXPORT:
              return true;
              break;
            case '新增':
              return true;
              break;
            case '修改':
              return true;
              break;
            case '详情':
              return true;
              break;
            case '删除':
              return true;
              break;
            default: return false;
          }
      },
    },
  mounted() {
  },
  components: {
    dataTable,
    mainModel,
    printmodal
  },
 
  created() {
    this.getColumns();
    this.getData(); 
    var data = {
        type: '单位类别'
    };
    apiBase.DoService(
        data,
        apiBase.getApiAdress().Code.GetType,
        res => {
            let cardata = res.data.content;
            this.UnttypeOptions = cardata.map(item => ({
                value: item.CodeValue,
                label: item.CodeName,
            }));
        });
    apiBase.DoService(
        {},
        apiBase.getApiAdress().Code.GetAddressMap,
        res => {
            let cardata = res.data.content;
            this.UntyuseroupOptions = cardata.map(item => ({
                value: item.ADDID,
                label: item.ADDNAME,
                ADDLEVEL: item.ADDLEVEL,
                ADDTOPID: item.ADDTOPID,
                children: item.Children.map(itemStr => ({
                value: itemStr.ADDID,
                label: itemStr.ADDNAME,
                ADDLEVEL: itemStr.ADDLEVEL,
                ADDTOPID: itemStr.ADDTOPID
                }))
            }));
        });
  }
};
</script>